/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.devel.designpatterns.visitor;

import com.devel.designpatterns.composite.Composite;
import com.devel.designpatterns.composite.Leaf;
import javax.swing.DefaultListModel;

/**
 *
 * @author wm.devel
 */
public class SwingListVisitor extends Visitor {
    
    private DefaultListModel model;
    private int level = 0;

    public SwingListVisitor(DefaultListModel model) {
        this.model = model;
        model.clear();
    }
    
    protected String getIndent() {
        StringBuilder s = new StringBuilder();
        for (int i = 0; i < level; i++)
            s.append("  ");
        return s.toString();
    }
    
    @Override
    public void visitCompositeStart(Composite composite) {
        model.addElement(getIndent() + composite);
        level++;
    }

    @Override
    public void visitCompositeEnd(Composite composite) {
        level--;
    }

    @Override
    public void visitLeaf(Leaf leaf) {
        model.addElement(getIndent() + leaf);
    }
}
